<?php

/*
 *
 *
 * This software has been made FREELY available by author for interest of the developers. 
 *      I hope this will help you in coding faster and robust php application
 *            You can modify and use it as you it suits you.
 *              Your comments, suggestions and corrections 
 *                          will be highly 
 *                           APPRECIATED. 
 *                                      Thanks, 
 *                                      Author
 * @author :   Roshan Budhathoki (contact@roshanbudhathoki.com);
 * @URL    :   <http://www.roshanbudhathoki.com>
 *
 */

class Session {
	//attributes
	private $session;
	private static $instance;
	
	public static function newInstance() {
		if (! self::$instance instanceof self) {
			self::$instance = new self ();
		}
		return self::$instance;
	}
	
	public function __construct() {
	}
	
	function session_start() {
		session_name ( 'myhighst' );
		session_start ();
		
		$this->session = $_SESSION;
		if ($this->_get ( 'messages' ) == '') {
			$this->_set ( 'messages', array () );
		}
		if ($this->_get ( 'keepForm' ) == '') {
			$this->_set ( 'keepForm', array () );
		}
	
	}
	
	function session_destroy() {
		session_destroy ();
	}
	
	function _set($key, $value) {
		$_SESSION [$key] = $value;
		$this->session [$key] = $value;
	}
	
	function _get($key) {
		if (! isset ( $this->session [$key] )) {
			return false;
		}
		
		return ($this->session [$key]);
	}
	
	function _drop($key) {
		unset ( $_SESSION [$key] );
		unset ( $this->session [$key] );
	
	}
	
	function _view() {
		print_r ( $this->session );
	
		//            echo "\n" ;
	}
	
	function _setMessage($key, $value, $type) {
		$messages = $this->_get ( 'messages' );
		$messages [$key] ['msg'] = $value;
		$messages [$key] ['type'] = $type;
		$this->_set ( 'messages', $messages );
	}
	
	function _getMessage($key) {
		$messages = $this->_get ( 'messages' );
		if (isset ( $messages [$key] )) {
			return ($messages [$key]);
		} else {
			return ('');
		}
	}
	
	function _dropMessage($key) {
		$messages = $this->_get ( 'messages' );
		unset ( $messages [$key] );
		$this->_set ( 'messages', $messages );
	}
	
	function _keepForm($key) {
		$aKeep = $this->_get ( 'keepForm' );
		$aKeep [$key] = 1;
		$this->_set ( 'keepForm', $aKeep );
	}
	
	function _dropKeepForm($key = '') {
		$aKeep = $this->_get ( 'keepForm' );
		if ($key != '') {
			unset ( $aKeep [$key] );
			$this->_set ( 'keepForm', $aKeep );
		} else {
			$this->_set ( 'keepForm', array () );
		}
	}
	
	function _setForm($key, $value) {
		$form = $this->_get ( 'form' );
		$form [$key] = $value;
		$this->_set ( 'form', $form );
	}
	
	function _getForm($key = '') {
		$form = $this->_get ( 'form' );
		if ($key != '') {
			if (isset ( $form [$key] )) {
				return ($form [$key]);
			} else {
				return ('');
			}
		} else {
			return $form;
		}
	}
	
	function _getKeepForm() {
		return $this->_get ( 'keepForm' );
	}
	
	function _viewMessage() {
		print_r ( $this->session ['messages'] );
	}
	
	function _viewForm() {
		print_r ( $_SESSION ['form'] );
	}
	
	function _viewKeep() {
		print_r ( $_SESSION ['keepForm'] );
	}
	
	function _clearVariables() {
		
		$this->_set ( 'messages', array () );
		$this->_set ( 'keepForm', array () );
		
		$form = $this->_get ( 'form' );
		$aKeep = $this->_get ( 'keepForm' );
		if (is_array ( $form )) {
			foreach ( $form as $key => $value ) {
				if (! isset ( $aKeep [$key] )) {
					unset ( $_SESSION ['form'] [$key] );
					unset ( $this->session ['form'] [$key] );
				}
			}
		}
	}
}
